package com.qianniu.napi.admin.service.akka;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.mapper.StoreStatMapper;
import com.qianniu.napi.admin.service.*;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.ObjectUtil;
import com.qianniu.napi.common.util.PictureUtil;
import com.qianniu.napi.common.util.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class NewsActor extends UntypedActor {

    private final static Logger logger = LoggerFactory.getLogger(NewsActor.class);
    private final static String rootsite = Constants.IMG_UPLOAD_HOST;
    private INewsService newsService = (INewsService) SpringUtil.getBean("newsServiceImpl");
    private IPyqService pyqService = (IPyqService) SpringUtil.getBean("pyqServiceImpl");
    private IRedisService redisService = (IRedisService) SpringUtil.getBean("redisServiceImpl");

    @Override
    public void onReceive(Object message) {

        if (message instanceof String) {
            String msg = message.toString();
            logger.debug(">>>>>>>>>>>>>>>>>>>>> NewsActor receive message:" + msg);

            // 格式 type+uid+toke
            String[] arr = msg.split("@");
            if (arr.length < 2) return;

            String type = arr[0];
            String id = arr[1];
            if (id == null || id.equals("null")) {
                return;
            }

            if (type.equals(Constants.AKKA_MSG_NEWS_VIEW)) {
                News news = newsService.selectById(id);
                news.setViews(news.getViews() == null ? 1 : news.getViews() + 1);
                news.setModifytime(new Date());
                news.updateById();

                if(news.getUid()==null)return;

                User ruser = redisService.getUser(news.getUid());

            } else if (type.equals(Constants.AKKA_MSG_STORE_ORDER)) {

            } else if (type.equals(Constants.AKKA_MSG_PYQ_VIEW)) {
                Pyq pyq = pyqService.selectById(id);
                pyq.setViews(pyq.getViews() == null ? 1 : pyq.getViews() + 1);
                pyq.setModifytime(new Date());
                pyq.updateById();
            }
        }
    }

}
